home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / prsr_lib / triv_wrt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-14  |  16.6 KB  |  418 lines

  1. /******************************************************************************
  2. * Triv_Wrt.c - Trivariate writing to files.                      *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Sep. 94.                          *
  5. ******************************************************************************/
  6.  
  7. #include <string.h>
  8. #include "prsr_loc.h"
  9.  
  10. /*****************************************************************************
  11. * DESCRIPTION:                                                               M
  12. * Generic routine to write trivariates to the given file.             M
  13. *   If Comment is NULL, no comment is printed, if "" only internal comment.  M
  14. *                                                                            *
  15. * PARAMETERS:                                                                M
  16. *   TVs:       To be saved in file f.                                        M
  17. *   FileName:  File name where output should go to.                          M
  18. *   Indent:    Column in which all printing starts at.                       M
  19. *   Comment:   Optional comment to describe the geometry.                    M
  20. *   ErrStr:    If failed, ErrStr will be set to describe the problem.        M
  21. *                                                                            *
  22. * RETURN VALUE:                                                              M
  23. *   int:        TRUE if succesful, FALSE otherwise.                          M
  24. *                                                                            *
  25. * KEYWORDS:                                                                  M
  26. *   TrivTVWriteToFile, files, write,trivariates                              M
  27. *****************************************************************************/
  28. int TrivTVWriteToFile(TrivTVStruct *TVs,
  29.               char *FileName,
  30.               int Indent,
  31.               char *Comment,
  32.               char **ErrStr)
  33. {
  34.     int RetVal = TRUE;
  35.     TrivTVStruct *NextTV;
  36.  
  37.     for (; TVs != NULL && RetVal; TVs = TVs -> Pnext) {
  38.     NextTV = TVs -> Pnext;      /* To make sure we dump one at a time. */
  39.     TVs -> Pnext = NULL;
  40.  
  41.     switch (TVs -> GType) {
  42.         case TRIV_TVBEZIER_TYPE:
  43.         RetVal = TrivBzrTVWriteToFile(TVs, FileName, Indent, Comment,
  44.                           ErrStr);
  45.         break;
  46.         case TRIV_TVBSPLINE_TYPE:
  47.         RetVal = TrivBspTVWriteToFile(TVs, FileName, Indent, Comment,
  48.                           ErrStr);
  49.         break;
  50.         default:
  51.         *ErrStr = "BSPLINE or BEZIER Token expected";
  52.         return FALSE;
  53.     }
  54.  
  55.     TVs -> Pnext = NextTV;
  56.     }
  57.  
  58.     return RetVal;
  59. }
  60.  
  61. /*****************************************************************************
  62. * DESCRIPTION:                                                               M
  63. * Generic routine to write trivariates to the given file.             M
  64. *   If Comment is NULL, no comment is printed, if "" only internal comment.  M
  65. *                                                                            *
  66. * PARAMETERS:                                                                M
  67. *   TVs:       To be saved in stream.                                        M
  68. *   Handler:   A handler to the open stream.                     M
  69. *   Indent:    Column in which all printing starts at.                       M
  70. *   Comment:   Optional comment to describe the geometry.                    M
  71. *   ErrStr:    If failed, ErrStr will be set to describe the problem.        M
  72. *                                                                            *
  73. * RETURN VALUE:                                                              M
  74. *   int:        TRUE if succesful, FALSE otherwise.                          M
  75. *                                                                            *
  76. * KEYWORDS:                                                                  M
  77. *   TrivTVWriteToFile2, files, write, trivariates                            M
  78. *****************************************************************************/
  79. int TrivTVWriteToFile2(TrivTVStruct *TVs,
  80.                int Handler,
  81.                int Indent,
  82.                char *Comment,
  83.                char **ErrStr)
  84. {
  85.     int RetVal = TRUE;
  86.     TrivTVStruct *NextTV;
  87.  
  88.     for (; TVs != NULL && RetVal; TVs = TVs -> Pnext) {
  89.     NextTV = TVs -> Pnext;         /* To make sure we dump one at a time. */
  90.     TVs -> Pnext = NULL;
  91.  
  92.     switch (TVs -> GType) {
  93.         case TRIV_TVBEZIER_TYPE:
  94.         RetVal = TrivBzrTVWriteToFile2(TVs, Handler, Indent, Comment,
  95.                            ErrStr);
  96.         break;
  97.         case TRIV_TVBSPLINE_TYPE:
  98.         RetVal = TrivBspTVWriteToFile2(TVs, Handler, Indent, Comment,
  99.                            ErrStr);
  100.         break;
  101.         default:
  102.         *ErrStr = "BSPLINE or BEZIER Token expected";
  103.         return FALSE;
  104.     }
  105.  
  106.     TVs -> Pnext = NextTV;
  107.     }
  108.  
  109.     return RetVal;
  110. }
  111.  
  112. /*****************************************************************************
  113. * DESCRIPTION:                                                               M
  114. * Generic routine to write curve(s) to the given file.                 M
  115. *   If Comment is NULL, no comment is printed, if "" only internal comment.  M
  116. *                                                                            *
  117. * PARAMETERS:                                                                M
  118. *   TVs:       To be saved in file f.                                        M
  119. *   f:         File descriptor where output should go to.                    M
  120. *   Indent:    Column in which all printing starts at.                       M
  121. *   Comment:   Optional comment to describe the geometry.                    M
  122. *   ErrStr:    If failed, ErrStr will be set to describe the problem.        M
  123. *                                                                            *
  124. * RETURN VALUE:                                                              M
  125. *   int:        TRUE if succesful, FALSE otherwise.                          M
  126. *                                                                            *
  127. * KEYWORDS:                                                                  M
  128. *   CagdCrvWriteToFile3, files, write                                        M
  129. *****************************************************************************/
  130. int TrivTVWriteToFile3(TrivTVStruct *TVs,
  131.                FILE *f,
  132.                int Indent,
  133.                char *Comment,
  134.                char **ErrStr)
  135. {
  136.     int
  137.     Handler = IritPrsrOpenStreamFromFile(f, FALSE, FALSE, FALSE),
  138.     i = TrivTVWriteToFile2(TVs, Handler, Indent, Comment, ErrStr);
  139.  
  140.     IritPrsrCloseStream(Handler, TRUE);
  141.  
  142.     return i;
  143. }
  144.  
  145. /*****************************************************************************
  146. * DESCRIPTION:                                                               M
  147. * Generic routine to write Bezier trivariates to the given file.         M
  148. *   If Comment is NULL, no comment is printed, if "" only internal comment.  M
  149. *                                                                            *
  150. * PARAMETERS:                                                                M
  151. *   TVs:       To be saved in file.                                          M
  152. *   FileName:  File name where output should go to.                          M
  153. *   Indent:    Column in which all printing starts at.                       M
  154. *   Comment:   Optional comment to describe the geometry.                    M
  155. *   ErrStr:    If failed, ErrStr will be set to describe the problem.        M
  156. *                                                                            *
  157. * RETURN VALUE:                                                              M
  158. *   int:        TRUE if succesful, FALSE otherwise.                          M
  159. *                                                                            *
  160. * KEYWORDS:                                                                  M
  161. *   TrivBzrTVWriteToFile, files, write,trivariates                           M
  162. *****************************************************************************/
  163. int TrivBzrTVWriteToFile(TrivTVStruct *TVs,
  164.              char *FileName,
  165.              int Indent,
  166.              char *Comment,
  167.              char **ErrStr)
  168. {
  169.     int i, Handler;
  170.     FILE *f;
  171.  
  172.     if ((f = fopen(FileName, "w")) == NULL) {
  173.     *ErrStr = "Fail to open file";
  174.     return FALSE;
  175.     }
  176.     Handler = IritPrsrOpenStreamFromFile(f, FALSE,
  177.                      IritPrsrSenseBinaryFile(FileName),
  178.                      FALSE);
  179.  
  180.     i = TrivBzrTVWriteToFile2(TVs, Handler, Indent, Comment, ErrStr);
  181.  
  182.     IritPrsrCloseStream(Handler, TRUE);
  183.  
  184.     return i;
  185. }
  186.  
  187. /*****************************************************************************
  188. * DESCRIPTION:                                                               M
  189. * Generic routine to write Bezier trivariates to the given file.         M
  190. *   If Comment is NULL, no comment is printed, if "" only internal comment.  M
  191. *                                                                            *
  192. * PARAMETERS:                                                                M
  193. *   TVs:       To be saved in stream.                                        M
  194. *   Handler:   A handler to the open stream.                     M
  195. *   Indent:    Column in which all printing starts at.                       M
  196. *   Comment:   Optional comment to describe the geometry.                    M
  197. *   ErrStr:    If failed, ErrStr will be set to describe the problem.        M
  198. *                                                                            *
  199. * RETURN VALUE:                                                              M
  200. *   int:        TRUE if succesful, FALSE otherwise.                          M
  201. *                                                                            *
  202. * KEYWORDS:                                                                  M
  203. *   TrivBzrTVWriteToFile2, files, write, trivariates                         M
  204. *****************************************************************************/
  205. int TrivBzrTVWriteToFile2(TrivTVStruct *TVs,
  206.               int Handler,
  207.               int Indent,
  208.               char *Comment,
  209.               char **ErrStr)
  210. {
  211.     int i, j, MaxCoord;
  212.  
  213.     if (Comment != NULL) {
  214.     _IPFprintf(Handler, Indent, "#\n");
  215.     _IPFprintf(Handler, Indent, "# cagd_lib - bezier TV(s) dump.\n");
  216.     _IPFprintf(Handler, Indent, "#\n");
  217.     _IPFprintf(Handler, Indent, "# %s\n", Comment);
  218.     _IPFprintf(Handler, Indent, "#\n");
  219.     }
  220.  
  221.     *ErrStr = NULL;
  222.  
  223.     while (TVs) {
  224.     MaxCoord = CAGD_NUM_OF_PT_COORD(TVs -> PType);
  225.  
  226.     if (TVs -> GType != TRIV_TVBEZIER_TYPE) {
  227.         *ErrStr = "Given tri-variate(s) is (are) not BEZIER trivariate(s)";
  228.         break;
  229.     }
  230.     _IPFprintf(Handler, Indent, "[TRIVAR BEZIER %d %d %d %c%c\n",
  231.         TVs -> ULength, TVs -> VLength, TVs -> WLength,
  232.         CAGD_IS_RATIONAL_PT(TVs -> PType) ? 'P' : 'E',
  233.         MaxCoord + '0');
  234.     Indent += 4;
  235.  
  236.     for (i = 0;
  237.          i < TVs -> VLength * TVs -> ULength * TVs -> WLength;
  238.          i++) {
  239.         if (i && i % TVs -> ULength == 0)
  240.         _IPFprintf(Handler, 0, "\n"); /* Put empty line between raws.*/
  241.         if (i && i % TVs -> UVPlane == 0)
  242.         _IPFprintf(Handler, 0, "\n");   /* Put 2 lns between planes. */
  243.  
  244.         _IPFprintf(Handler, Indent, "[");
  245.         if (CAGD_IS_RATIONAL_PT(TVs -> PType))
  246.         _IPFprintf(Handler, 0, "%s ",
  247.                _IPReal2Str(TVs -> Points[0][i]));
  248.         for (j = 1; j <= MaxCoord; j++) {
  249.         _IPFprintf(Handler, 0, "%s",
  250.                _IPReal2Str(TVs -> Points[j][i]));
  251.         if (j < MaxCoord)
  252.             _IPFprintf(Handler, 0, " ");
  253.         }
  254.         _IPFprintf(Handler, 0, "]\n");
  255.     }
  256.  
  257.     Indent -= 4;
  258.     _IPFprintf(Handler, Indent, "]\n");
  259.  
  260.     TVs = TVs -> Pnext;
  261.     }
  262.  
  263.     return *ErrStr == NULL;
  264. }
  265.  
  266. /*****************************************************************************
  267. * DESCRIPTION:                                                               M
  268. * Generic routine to write Bspline trivariates to the given file.         M
  269. *   If Comment is NULL, no comment is printed, if "" only internal comment.  M
  270. *                                                                            *
  271. * PARAMETERS:                                                                M
  272. *   TVs:       To be saved in file f.                                        M
  273. *   FileName:  File name where output should go to.                          M
  274. *   Indent:    Column in which all printing starts at.                       M
  275. *   Comment:   Optional comment to describe the geometry.                    M
  276. *   ErrStr:    If failed, ErrStr will be set to describe the problem.        M
  277. *                                                                            *
  278. * RETURN VALUE:                                                              M
  279. *   int:        TRUE if succesful, FALSE otherwise.                          M
  280. *                                                                            *
  281. * KEYWORDS:                                                                  M
  282. *   TrivBzrTVWriteToFile, files, write,trivariates                           M
  283. *****************************************************************************/
  284. int TrivBspTVWriteToFile(TrivTVStruct *TVs,
  285.              char *FileName,
  286.              int Indent,
  287.              char *Comment,
  288.              char **ErrStr)
  289. {
  290.     int i, Handler;
  291.     FILE *f;
  292.  
  293.     if ((f = fopen(FileName, "w")) == NULL) {
  294.     *ErrStr = "Fail to open file";
  295.     return FALSE;
  296.     }
  297.     Handler = IritPrsrOpenStreamFromFile(f, FALSE,
  298.                      IritPrsrSenseBinaryFile(FileName),
  299.                      FALSE);
  300.  
  301.     i = TrivBspTVWriteToFile2(TVs, Handler, Indent, Comment, ErrStr);
  302.  
  303.     IritPrsrCloseStream(Handler, TRUE);
  304.  
  305.     return i;
  306. }
  307.  
  308. /*****************************************************************************
  309. * DESCRIPTION:                                                               M
  310. * Generic routine to write Bspline trivariates to the given file.         M
  311. *   If Comment is NULL, no comment is printed, if "" only internal comment.  M
  312. *                                                                            *
  313. * PARAMETERS:                                                                M
  314. *   TVs:       To be saved in stream.                                        M
  315. *   Handler:   A handler to the open stream.                     M
  316. *   Indent:    Column in which all printing starts at.                       M
  317. *   Comment:   Optional comment to describe the geometry.                    M
  318. *   ErrStr:    If failed, ErrStr will be set to describe the problem.        M
  319. *                                                                            *
  320. * RETURN VALUE:                                                              M
  321. *   int:        TRUE if succesful, FALSE otherwise.                          M
  322. *                                                                            *
  323. * KEYWORDS:                                                                  M
  324. *   TrivBspTVWriteToFile2, files, write, trivariates                         M
  325. *****************************************************************************/
  326. int TrivBspTVWriteToFile2(TrivTVStruct *TVs,
  327.               int Handler,
  328.               int Indent,
  329.               char *Comment,
  330.               char **ErrStr)
  331. {
  332.     int i, j, Len, MaxCoord;
  333.     CagdRType *KnotVector;
  334.  
  335.     if (Comment != NULL) {
  336.     _IPFprintf(Handler, Indent, "#\n");
  337.     _IPFprintf(Handler, Indent, "# cagd_lib - bspline TV(s) dump.\n");
  338.     _IPFprintf(Handler, Indent, "#\n");
  339.     _IPFprintf(Handler, Indent, "# %s\n", Comment);
  340.     _IPFprintf(Handler, Indent, "#\n");
  341.     }
  342.  
  343.     *ErrStr = NULL;
  344.  
  345.     while (TVs) {
  346.     MaxCoord = CAGD_NUM_OF_PT_COORD(TVs -> PType);
  347.  
  348.     if (TVs -> GType != TRIV_TVBSPLINE_TYPE) {
  349.         *ErrStr = "Given tri-variate(s) is (are) not Bspline trivariate(s)";
  350.         break;
  351.     }
  352.     _IPFprintf(Handler, Indent, "[TRIVAR BSPLINE %d %d %d %d %d %d %c%c\n",
  353.         TVs -> ULength, TVs -> VLength, TVs -> WLength,
  354.         TVs -> UOrder, TVs -> VOrder, TVs -> WOrder,
  355.         CAGD_IS_RATIONAL_PT(TVs -> PType) ? 'P' : 'E',
  356.         MaxCoord + '0');
  357.     Indent += 4;
  358.  
  359.     /* Put out the knot vectors: */
  360.     for (i = 0; i < 3; i++) {
  361.         switch (i) {
  362.         case 0:
  363.             KnotVector = TVs -> UKnotVector;
  364.             Len = TVs -> ULength + TVs -> UOrder;
  365.             break;
  366.         case 1:
  367.             KnotVector = TVs -> VKnotVector;
  368.             Len = TVs -> VLength + TVs -> VOrder;
  369.             break;
  370.         default:
  371.         case 2:
  372.             KnotVector = TVs -> WKnotVector;
  373.             Len = TVs -> WLength + TVs -> WOrder;
  374.             break;
  375.         }
  376.  
  377.         _IPFprintf(Handler, Indent, "[KV");
  378.         for (j = 0; j < Len; j++) {
  379.         if (j && j % MAX_KNOTS_PER_LINE == 0) {
  380.             _IPFprintf(Handler, 0, "\n");
  381.             _IPFprintf(Handler, Indent + 4, "");
  382.         }
  383.         _IPFprintf(Handler, 0, " %s", _IPReal2Str(KnotVector[j]));
  384.         }
  385.         _IPFprintf(Handler, 0, "]\n");
  386.     }
  387.  
  388.     /* Put out the control mesh. */
  389.     for (i = 0;
  390.          i < TVs -> VLength * TVs -> ULength * TVs -> WLength;
  391.          i++) {
  392.         if (i && i % TVs -> ULength == 0)
  393.         _IPFprintf(Handler, 0, "\n"); /* Put empty line between raws.*/
  394.         if (i && i % TVs -> UVPlane == 0)
  395.         _IPFprintf(Handler, 0, "\n");   /* Put 2 lns between planes. */
  396.  
  397.         _IPFprintf(Handler, Indent, "[");
  398.         if (CAGD_IS_RATIONAL_PT(TVs -> PType))
  399.         _IPFprintf(Handler, 0, "%s ",
  400.                _IPReal2Str(TVs -> Points[0][i]));
  401.         for (j = 1; j <= MaxCoord; j++) {
  402.         _IPFprintf(Handler, 0, "%s",
  403.                _IPReal2Str(TVs -> Points[j][i]));
  404.         if (j < MaxCoord)
  405.             _IPFprintf(Handler, 0, " ");
  406.         }
  407.         _IPFprintf(Handler, 0, "]\n");
  408.     }
  409.  
  410.     Indent -= 4;
  411.     _IPFprintf(Handler, Indent, "]\n");
  412.  
  413.     TVs = TVs -> Pnext;
  414.     }
  415.  
  416.     return *ErrStr == NULL;
  417. }
  418.